package com.gzwy;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class Main {
    public static void main(String[] args) throws RegionConflictException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, InvalidPortException, InvalidEndpointException {
        String endpoint = "http://172.168.200.223:9000";
        String bucket = "markdown";
        MinioClient minioClient = new MinioClient(endpoint, "admin", "gdztyy@2021!");

        // 检查存储桶是否已经存在
        boolean isExist = minioClient.bucketExists(bucket);
        if (isExist) {
            //System.out.println("Bucket already exists.");
        } else {
            // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
            minioClient.makeBucket(bucket);
        }
        
        System.out.println("Upload Success:");
        for (int i = 0; i < args.length; i++) {
            String filename = UUID.randomUUID()+"."+FileNameUtil.extName(args[i]);
            File file = FileUtil.file(args[i]);
            minioClient.putObject(bucket, filename, new FileInputStream(file),new PutObjectOptions(file.length(),-1));
            System.out.println(endpoint+ "/"+bucket+"/"+filename);
        }

    }
}
